<?php
/**
 * App_Form
 *
 * @category    IPB_CMS
 * @package     App_Form
 * @copyright   Copyright (c) 2009 IPB CMS <http://ipb-cms.googlecode.com/>
 * @license     http://www.gnu.org/licenses/gpl-3.0.html GNU General Public License
 * @author      Henrique Moody <henriquemoody@gmail.com>
 */
class App_Form extends Zend_Form
{

    /**
     * Name of the form that the current form belongs (if any)
     *
     * @var string
     */
    protected $_belongTo;

    /**
     * Constructor
     *
     * Registers form view helper as decorator
     *
     * @param mixed $options
     * @return void
     */
    public function __construct($options = null)
    {
        $this->addPrefixPath('App_Form_Decorator', 'App/Form/Decorator', 'decorator')
             ->addPrefixPath('App_Form_Element', 'App/Form/Element', 'element')
             ->addElementPrefixPath('App_Filter', 'App/Filter', 'filter')
             ->addElementPrefixPath('App_Form_Decorator', 'App/Form/Decorator', 'decorator')
             ->addDisplayGroupPrefixPath('App_Form_Decorator', 'App/Form/Decorator');
        parent::__construct($options);
    }

    /**
     * Add a form group/subform
     *
     * @param Zend_Form $form
     * @param string $name
     * @param integer $order
     * @return Zend_Form Fluent interface, returns self
     */
    public function addSubForm(Zend_Form $form, $name, $order = null)
    {
        $form->setBelongsTo($this->getName());
        if(!$form instanceof Zend_Form_SubForm) {
            $form->setDecorators(array(
                'FormElements',
                array('HtmlTag', array('tag' => 'dl')),
                'Fieldset',
                'DtDdWrapper'
            ));
            $form->setIsArray(true);
            $form->removeElement($form->getName() . '_submit');
        }
        return parent::addSubForm($form, $name, $order);
    }

    /**
     * Set the name of the form that the current form belongs
     *
     * @param string $name
     */
    public function setBelongsTo($name)
    {
        $this->_belongTo = $name;
    }

    /**
     * Returns the name of the form that the current form belongs
     *
     * @return string|null
     */
    public function getBelongsTo()
    {
        return $this->_belongTo;
    }

    /**
     * Get name attribute
     *
     * If the name of form does not exists creates one by the class name
     *
     * @return string
     */
    public function getName()
    {
        if ( ! $this->getAttrib('name') ) {
            $name = strtolower(get_class($this));
            $this->setName($name);
        }
        return $this->getAttrib('name');
    }

    /**
     * Load the default decorators
     *
     * @return void
     */
    public function loadDefaultDecorators()
    {
        if ($this->loadDefaultDecoratorsIsDisabled()) {
            return;
        }
        $decorators = $this->getDecorators();
        if ( empty($decorators) ) {
            $this->addDecorator('FormElements')
                 ->addDecorator('HtmlTag', array('tag' => 'dl', 'class' => 'ipb_form'))
                 ->addDecorator('Form')
                 ->addDecorator('JQueryValidation');
        }
    }

}
